<template>
  <div class="app-container bg-gray">
    <el-card class="box-1">
      <div class="header">
        <div>
          <el-select v-model="searchTerms['activity_type']" placeholder="活动类型" class="select" clearable filterable @change="getData">
            <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
          <el-select v-model="searchTerms.status" placeholder="活动状态" class="select" clearable @change="getData">
            <el-option :value="1" label="报名中" />
            <el-option :value="2" label="报名截止" />
            <el-option :value="3" label="活动进行中" />
            <el-option :value="4" label="活动已结束" />
            <el-option :value="0" label="已取消" />
          </el-select>
          <el-date-picker
            v-model="searchTerms.date"
            :editable="false"
            class="picker"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            @change="getData"/>
          <el-input v-model="searchTerms.search" placeholder="请输入活动名称" class="search-input" @change="getData" />
          <el-button type="success" class="do-btn" icon="el-icon-search" @click="getData">搜索</el-button>
        </div>
        <div>
          <el-button type="primary" icon="el-icon-plus" @click="toCreateActivity">创建活动</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="dialogVisible=true">从已有活动创建</el-button>
        </div>
      </div>
      <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
        <el-table-column prop="id" label="ID" width="100" header-align="center" align="center" sortable />
        <el-table-column prop="name" label="名称" min-width="160" header-align="center" align="center" />
        <el-table-column prop="host_party" label="发起人" min-width="120" header-align="center" align="center" />
        <el-table-column prop="total_number" label="已报名" width="120" header-align="center" align="center" />
        <el-table-column label="活动时间" min-width="200" header-align="center" align="center">
          <template slot-scope="scope">
            {{ parseTime(scope.row['start_time'], '{y}-{m}-{d} {h}:{i}') +' 至 '+ parseTime(scope.row['end_time'], '{y}-{m}-{d} {h}:{i}') }}
          </template>
        </el-table-column>
        <el-table-column prop="buy_time" label="更新时间" min-width="160" header-align="center" align="center">
          <template slot-scope="scope">
            {{ scope.row['updated_at']?parseTime(scope.row['updated_at'], '{y}-{m}-{d}'):'--' }}
          </template>
        </el-table-column>
        <el-table-column prop="activity_status" label="活动状态" width="160" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            <el-tag v-if="scope.row['activity_status']===0" type="danger">已取消</el-tag>
            <el-tag v-if="scope.row['activity_status']===1" type="primary">报名中</el-tag>
            <el-tag v-if="scope.row['activity_status']===2" type="warning">报名截止</el-tag>
            <el-tag v-if="scope.row['activity_status']===3" type="success">活动进行中</el-tag>
            <el-tag v-if="scope.row['activity_status']===4" type="info">活动已结束</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button type="primary" size="small" icon="el-icon-search" circle @click="toInfo(scope.row)"/>
            <el-button v-if="scope.row['activity_status']===1||scope.row['activity_status']===2" type="primary" class="bg-green" size="small" icon="el-icon-edit" circle @click="toModify(scope.row)"/>
            <el-button
              v-if="scope.row['activity_status']===4||scope.row['activity_status']===0"
              type="danger"
              size="small"
              icon="el-icon-delete"
              circle
              @click="toDelete(scope.row)"/>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="pageSize"
        :page-sizes="pageSizes"
        :total="total"
        class="pagination"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"/>
    </el-card>

    <el-dialog :visible.sync="dialogVisible" title="选择活动" width="500px" class="dialog" center @close="resetDialog('form')">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="活动" prop="activityId">
          <el-select v-model="form.activityId" filterable placeholder="请选择类型">
            <el-option v-for="item in activityList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { handleSortChange, handleSizeChange, handleCurrentChange, parseTime } from '@/utils/table'

export default {
  data() {
    return {
      downloadLoading: false,
      searchTerms: {
        activity_type: '',
        state: '',
        date: [],
        search: ''
      },

      typeList: [], // 活动类型

      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 380,
      tableLoading: false,
      multipleSelection: [],
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: [],

      // 全部活动列表
      activityList: [],
      dialogVisible: false,
      form: {
        activityId: ''
      },
      rules: {
        activityId: [{ required: true, message: '请输入姓名', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getData()
    this.getTypeList()
    this.getActivityList()
  },
  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange, parseTime,
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'activity/getActivityList'
      const data = {
        page_no: this.currentPage,
        page_size: this.pageSize,
        search: this.searchTerms.search,
        status: this.searchTerms.status,
        activity_type: this.searchTerms.activity_type
      }
      this.searchTerms.date && this.searchTerms.date.length !== 0 && (data.start_time = this.searchTerms.date[0] + ' 00:00:00', data.end_time = this.searchTerms.date[1] + ' 23:59:59')
      order.sortName && (data.sortName = order.sortName)
      order.sortOrder && (data.sortOrder = order.sortOrder)
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data.rows
        this.total = res.data.total
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    /**
     *  获取活动类型列表
     */
    getTypeList() {
      const url = 'activity/getTypeList'
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.typeList = res.data
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    /**
     *  获取所有活动列表
     */
    getActivityList() {
      const url = 'activity/activityAllList'
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.activityList = res.data
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    toInfo(row) {
      this.$router.push({
        path: '/activity/details',
        query: { id: row.id }
      })
    },

    toModify(row) {
      this.$router.push({
        path: '/activity/modify',
        query: { id: row.id }
      })
    },

    toDelete(row) {
      this.$confirm('此操作将删除该活动, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = 'activity/activityDel/' + row.id
        this.$store.dispatch('GetConnect', { url }).then(res => {
          this.$message.success(res.msg)
          this.getData()
          this.getActivityList()
        }).catch(e => {
          this.$message.error(e.msg + ',请刷新或联系管理员')
        })
      })
    },

    toCreateActivity() {
      this.$router.push({
        path: '/activity/create'
      })
    },
    /**
     * 表单提交
     * 添加客户
     */
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$router.push({
            path: '/activity/modify',
            query: { fromId: this.form['activityId'] }
          })
        } else {
          this.$message.error('提交失败,请检查必填项')
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../styles/index.scss';
.box-1 {
  .model {
    .el-card {
      color: #fff;
      background-color: $panGreen;
      span {
        font-size: 14px;
      }
      div {
        font-size: 24px;
        margin-top: 10px;
      }
    }
  }
  .header {
    @include flex-row(center, space-between);
    flex-wrap: wrap;
    padding-bottom: 15px;
    div {
      @include flex-row(center, flex-start);
      flex-wrap: wrap;
      .select {
        padding-right: 15px;
        width: 150px;
      }
      .picker {
        margin-right: 15px;
        width: 300px;
      }
      .do-btn {
        @extend .bg-green;
        margin-left: 6px;
      }
      .search-input {
        width: 180px;
      }
    }
  }
}
</style>

